// DinputDialog.cpp : implementation file
//

#include "stdafx.h"
#include "Logic.h"
#include "DinputDialog.h"
#include "dinput.h"


// CDinputDialog dialog

IMPLEMENT_DYNAMIC(CDinputDialog, CDialog)

CDinputDialog::CDinputDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CDinputDialog::IDD, pParent)
{

}

CDinputDialog::~CDinputDialog()
{
}

void CDinputDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDinputDialog, CDialog)
	ON_WM_CLOSE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_STARTBUTTON, &CDinputDialog::OnBnClickedStartbutton)
END_MESSAGE_MAP()


// CDinputDialog message handlers

void CDinputDialog::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	ShowWindow(SW_HIDE);
	//CDialog::OnClose();
}

void CDinputDialog::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==0x100)
	{
		KeyInfo data[2];
		data[0].dwOfs=DIK_1;
		data[0].dwData=0x80;
		data[1].dwOfs=DIK_1;
		data[1].dwData=0x0;

		if(clientPipe->IsServerAvailable())
		{
			if(!clientPipe->Write(data,2))
			{
				::AfxMessageBox(_T("Write file fail."));
			}
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CDinputDialog::OnBnClickedStartbutton()
{
	// TODO: Add your control notification handler code here
	clientPipe= new NamedPipeClient(_T("\\\\.\\pipe\\DInput-Hook"));
	
	while(!clientPipe->IsServerAvailable())
	{
		if(!clientPipe->Connect())
		{
			::AfxMessageBox(_T("Connect fail."));
		}
		::AfxMessageBox(_T("Server is not available."));
	}
	SetTimer(0x100,1000,NULL);
}
